package com.design.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.design.reggie.common.BaseContext;
import com.design.reggie.common.Result;
import com.design.reggie.entity.ShoppingCart;
import com.design.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @return 购物车列表
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {

//        log.info("------提供的id是：{}", BaseContext.getCurrentId().toString());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseContext.getCurrentId() != null, ShoppingCart::getUserId, BaseContext.getCurrentId());

        // 最晚下单的 菜品或套餐在购物车中最先展示
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return Result.success(list);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        // 当前是谁的购物车
        Long userId = BaseContext.getCurrentId();

        // 菜品id和套餐id必然有一个是空
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();


        // 查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }
        if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        // select * from shopping_cart where user_id = ? and dish_id = ?/setmeal_id = ?
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one == null) {
            // 购物车中不存在当前菜品（套餐），直接新增一份
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //因为这个分支的one是null，所以要覆盖一下
            one = shoppingCart;
        } else {
            // 存在，则份数+1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        }

        return Result.success(one);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> clean() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);
        return Result.success("清空成功");
    }

    /**
     * 减少购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        }
        if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // 当前份数
        Integer number = one.getNumber();
        if (number - 1 > 0) {
            // -1
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
            return Result.success(one);
        } else if (number - 1 == 0) {
            one.setNumber(0);
            shoppingCartService.updateById(one);
            clean();
            return Result.success(one);
        } else {
            return Result.error("数量不得小于1份");
        }

    }

}
